package org.sun.basic.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.sun.basic.dao.IGroupDao;
import org.sun.basic.model.Group;

@Service("groupService")
public class GroupService implements IGroupService {
	
	@Inject
	private IGroupDao groupDao;
	

	@Override
	public Group createGroup(Group g) {
		return groupDao.add(g);
	}

	@Override
	public Group getGroup(String gid) {
		
		return groupDao.load(gid);
	}

	@Override
	public List<Group> getGroupByUserId(String uid) {
		String sql="select grp_id from usr_grp_rel where usr_id=?";
		String hql="from Group where id in(:ids)";
		List<Object[]> gids=groupDao.queryList(sql, uid);
		List<Object> gls=new ArrayList<Object>();
		for(Object o:gids){
			gls.add(o);
		}
		Map<String,Object> alias=new HashMap<String, Object>();
		alias.put("ids", gls);
		return groupDao.listByAlias(hql, alias);
	}

	@Override
	public void modifyGroup(Group g) {
		groupDao.update(g);
	}

}
